var express = require('express');
var router = express.Router();
var request = require('request');

var CommonJS = require('../../common/common');
var DBConfig = require('../../db/dbconfig');
var Wx_config = require('../../wx_config/wx_config');
var mysql = require('mysql');
var pool = mysql.createPool(DBConfig.mysql2);
var db = require('../../db/mysql/pool');

// 获取充值列表
router.get('/jkyx/pc/get_recharge_list', function(req, res, next) {
	var param = req.query || req.params; 
	// 手机
	var account = param.account || 0;

    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.pc_recharge_list,function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100',
	     			results: results
	     		}));
			}
		})
	})
});
// 新增一个充值
router.get('/jkyx/pc/recharge_add', function(req, res, next) {
	var param = req.query || req.params; 
	// 充值金额
	var recharge_money = param.recharge_money || '';
	// 赠送金额
	var gift_money = param.gift_money || '';
	// 充值开始时间
	var start_time = param.start_time || '';
	// 充值结束时间
	var end_time = param.end_time || '';
	// 充值新建时间
	var create_time = CommonJS.getNowFormatDate();

    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.pc_recharge_add,[recharge_money,gift_money,start_time,end_time,create_time],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				res.end(JSON.stringify({
	     			msg: '登录成功',
	     			status:'100',
	     			results: results
	     		}));
			}
		})
	})
});
// 删除一个充值
router.get('/jkyx/pc/recharge_delete', function(req, res, next) {
	var param = req.query || req.params; 
	// 充值ID
	var id = param.id || '';

    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.pc_recharge_delete,[id],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				res.end(JSON.stringify({
	     			msg: '登录成功',
	     			status:'100',
	     			results: results
	     		}));
			}
		})
	})
});

module.exports = router;